package servlet;

import java.util.List;

import javabean.Factory;
import javabean.book.BookComment;
import javabean.user.User;


public class BookCommentServlet extends BaseServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8538076142868886263L;
	
	public void service() throws Exception{
		String uid = (String)request.getSession().getAttribute("uid");
		if(uid!=null){
			java.text.SimpleDateFormat dateFormatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String bctime = dateFormatter.format(new java.util.Date());	//取得当前时间
			Long bid = Long.parseLong(request.getParameter("bid"));
			String score1 = request.getParameter("score");
			String bccontents = request.getParameter("bccontents");
			BookComment bookComment = new BookComment();
			bookComment.setUid(uid);
			bookComment.setBctime(bctime);
			bookComment.setBid(bid);
			bookComment.setBccontents(bccontents);
			//分数评价
			if(score1!=null && !"".equals(score1)){
				int score = Integer.parseInt(score1);
				bookComment.setScore(score);
				try{
					if(Factory.getBookDAOInstance().giveBookScore(bookComment)){
						response.getWriter().write("{\"isLogin\":true,\"success\":true,\"msg\":\"评分成功\"}");
					}else{
						response.getWriter().write("{\"isLogin\":true,\"success\":false,\"msg\":\"评分失败\"}");
					}
				}catch(Exception ex){
					System.out.println("数据库出错了");
				}
			//评论
			}else if(bccontents!=null && !"".equals(bccontents)){
				try{
					long bcid = Factory.getBookDAOInstance().giveBookComment(bookComment);
					if(bcid!=0){
						List<User> all = null;
						try{
							all = Factory.getUserDAOInstance().queryAllByUidOrEmail(uid);
						}catch(Exception ex){}
						User user = (User)all.get(0);
						response.getWriter().write("{\"isLogin\":true,\"success\":true,\"bcid\":\""+bcid+"\",\"uid\":\""+user.getUid()+
								"\",\"uname\":\""+user.getUname()+"\",\"upic\":\""+user.getPic()+"\",\"introduce\":\""+
								user.getIntroduce()+"\",\"bctime\":\""+bctime+"\",\"msg\":\"评论成功\"}");
					}else{
						response.getWriter().write("{\"isLogin\":true,\"success\":false,\"msg\":\"评论失败\"}");
					}
				}catch(Exception ex){
					System.out.println("数据库出错了");
				}
			}	
		}else{
			response.getWriter().write("{\"isLogin\":false,\"success\":false,\"msg\":\"你还未登陆\"}");
		}
	}
}
